﻿using System;
using System.IO;
using System.Text.RegularExpressions;

namespace KnifeZ.Unity.Helpers
{
    /// <summary>
    /// 小刀助手
    /// </summary>
    public class KnifeHelper
    {
        /// <summary>
        /// 获取指定范围的随机日期
        /// </summary>
        /// <param name="day">减去的最大天数</param>
        /// <returns>间隔日期之间的 随机日期</returns>
        public static DateTime GetRandomTime(int day, int x = 8, int y = 18)
        {
            Random random = new Random();
            var h = random.Next(x, y);
            var m = random.Next(0, 60);
            var s = random.Next(0, 60);
            //先把时间转换成年月日的字符串格式 在转换成时间（把时间格式转换成日期格式）
            DateTime dateTime = DateTime.Now;
            var add = dateTime.AddDays(-random.Next(0, day));
            //减去的天数加上随机生成的时间得到我们自己需要的时间
            var newDateTime = Convert.ToDateTime(add.ToString("yyyy-MM-dd 00:00:00")) + TimeSpan.Parse(h + ":" + m + ":" + s);
            //返回需要的时间
            return newDateTime;
        }

        /// <summary>
        /// 相对路径转绝对路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ConvertToAbsolutePath(string path)
        {
            //绝对路径
            if (path.Contains(":\\"))
            {
                return path;
            }
            return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
        }
        /// <summary>
        /// 绝对路径转相对路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ConvertToRelativePath(string path)
        {
            if (path.StartsWith(@"/"))
            {
                return path;
            }
            return path.Replace(AppDomain.CurrentDomain.BaseDirectory, "").Replace(@"\", @"/");
        }

        /// <summary>
        /// 正则移除所有HTML标签
        /// </summary>
        /// <param name="html"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string RemoveHtmlTags(string html, int length = 0)
        {
            string strText = Regex.Replace(html, "<[^>]+>", "");
            strText = Regex.Replace(strText, "&[^;]+;", "");
            if (length > 0 && strText.Length > length)
                return strText.Substring(0, length);

            return strText;
        }
        /// <summary>
        /// 简单记录日志到本地文件
        /// </summary>
        /// <param name="info">记录信息</param>
        /// <param name="logLevel">日志级别</param>
        /// <param name="path">日志路径(相对路径)</param>
        public static void Log(string info, string logLevel = "Debug", string path = "logs/log.txt")
        {
            path = ConvertToAbsolutePath(path);
            FileInfo fi = new FileInfo(path);
            var di = fi.Directory;
            if (!di.Exists)
            {
                di.Create();
            }

            using (StreamWriter sw = new StreamWriter(path, true))
            {
                sw.WriteLine(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss]") + logLevel + " -- " + info);
            }
        }

        ///<summary>
        ///生成随机字符串 
        ///</summary>
        ///<param name="length">目标字符串的长度</param>
        ///<param name="useNumber">是否包含数字，1=包含，默认为包含</param>
        ///<param name="useLowerCase">是否包含小写字母，1=包含，默认为包含</param>
        ///<param name="useUpperCase">是否包含大写字母，1=包含，默认为包含</param>
        ///<param name="useSpecialCase">是否包含特殊字符，1=包含，默认为不包含</param>
        ///<param name="custom">要包含的自定义字符，直接输入要包含的字符列表</param>
        ///<returns>指定长度的随机字符串</returns>
        public static string GetRandomString(int length, bool useNumber = true, bool useLowerCase = true, bool useUpperCase = true, bool useSpecialCase = false, string custom = "")
        {
            byte[] b = new byte[4];
            new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
            Random r = new Random(BitConverter.ToInt32(b, 0));
            string s = null, str = custom;
            if (useNumber == true) { str += "0123456789"; }
            if (useLowerCase == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
            if (useUpperCase == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
            if (useSpecialCase == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
            for (int i = 0; i < length; i++)
            {
                s += str.Substring(r.Next(0, str.Length - 1), 1);
            }
            return s;
        }


    }
}
